import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/community_business/community/community_module_logic.dart';
import 'package:tiktokadult/hj_page/home_business/widget/publist_entry_alert.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/widget/keep_alive_widget.dart';

import '../../../tools_base/indicator/custom_tab_indicator.dart';
import 'community_tab_view.dart';

enum CommunityModuleType {
  community, //社区
  pics, //图集
  source, //资源
  noval; //小说
}

class CommunityModulePage extends StatefulWidget {
  final CommunityModuleType moduleType;
  final int defaultIndex;
  const CommunityModulePage(this.moduleType, {super.key, this.defaultIndex = 0});

  @override
  State<CommunityModulePage> createState() => _CommunityModulePageState();
}

class _CommunityModulePageState extends State<CommunityModulePage> with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<CommunityMainModuleLogic>(
      init: CommunityMainModuleLogic(widget.defaultIndex),
      builder: (controller) {
        if (controller.modules.isEmpty) return CErrorWidget();
        return Stack(
          children: [
            Column(
              children: [
                Container(
                  child: TabBar(
                    labelPadding: EdgeInsets.zero,
                    padding: EdgeInsets.zero,
                    tabAlignment: TabAlignment.start,
                    indicator: CustomIndicator(
                      height: 4.h,
                      width: 16.w,
                      useGradient: true,
                      gradientColors: [
                        Color(0xffE1351F),
                        Color(0xffE1351F),
                      ],
                      offsetY: 6,
                    ),
                    indicatorWeight: 1,
                    unselectedLabelStyle: TextStyle(color: Color(0x73FFFFFF), fontSize: 16),
                    labelStyle: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
                    isScrollable: true,
                    tabs: controller.modules
                        .map(
                          (e) => Padding(
                            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 13.h),
                            child: Text(e.moduleName ?? ''),
                          ),
                        )
                        .toList(),
                    controller: controller.tabController,
                  ),
                ),
                Expanded(
                  child: TabBarView(
                    controller: controller.tabController,
                    children: controller.modules
                        .asMap()
                        .map(
                          (key, value) => MapEntry(
                            key,
                            CommunityTabView(
                              model: value,
                              index: key,
                              cancelCallBack: () {
                                controller.tabController.index = controller.tabController.previousIndex;
                              },
                            ).keepAlive,
                          ),
                        )
                        .values
                        .toList(),
                  ),
                )
              ],
            ),
            Positioned(
              right: 20,
              bottom: 50,
              child: PublishButton(
                entry: PublishEntry.community,
              ),
            ),
          ],
        );
      },
    );
  }
}
